import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author：zzq
 * @createTime：2025/10/08 20:46
 * @description：使用高德地图API查询天气
 */
public class WeatherReporter {
    public static void main(String[] args) {
        // 高德天气API服务地址
        String host = "https://restapi.amap.com";
        String path = "/v3/weather/weatherInfo";
        // 替换为你自己的高德key
        String key = "ff8a6878abc51bd062cbe5ed1c527c91";

        Map<String, String> querys = new HashMap<>();
        // 设置请求参数
        querys.put("key", key);
        querys.put("city", "110101"); // 示例城市编码
        querys.put("extensions", "all"); // 获取实况天气

        try {
            // 构建查询字符串
            StringBuilder queryString = new StringBuilder();
            for (Map.Entry<String, String> entry : querys.entrySet()) {
                if (queryString.length() > 0) {
                    queryString.append("&");
                }
                queryString.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8));
                queryString.append("=");
                queryString.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
            }

            // 构建完整的URL
            String url = host + path + "?" + queryString.toString();

            // 创建HttpClient实例
            HttpClient client = HttpClient.newHttpClient();

            // 创建HttpRequest
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(url))
                    .header("Content-Type", "application/json")
                    .GET()
                    .build();

            // 发送请求并获取响应
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            // 检查响应状态码
            if (response.statusCode() == 200) {
                // 使用Jackson解析JSON响应
                ObjectMapper objectMapper = new ObjectMapper();
                WeatherResponse weatherResponse = objectMapper.readValue(response.body(), WeatherResponse.class);

                // 输出天气信息到控制台
                System.out.println("请求状态: " + weatherResponse.getStatus());
                System.out.println("返回信息: " + weatherResponse.getInfo());

                if ("1".equals(weatherResponse.getStatus())) {
                    // 实况天气信息
                    if (weatherResponse.getLives() != null && !weatherResponse.getLives().isEmpty()) {
                        System.out.println("=== 实况天气 ===");
                        for (WeatherResponse.LiveWeather live : weatherResponse.getLives()) {
                            System.out.println(live.toString());
                        }
                    }

                    // 预报天气信息
                    if (weatherResponse.getForecasts() != null && !weatherResponse.getForecasts().isEmpty()) {
                        System.out.println("=== 预报天气 ===");
                        WeatherResponse.ForecastWeather forecast = weatherResponse.getForecasts().get(0);
                        if (forecast.getCasts() != null) {
                            for (WeatherResponse.ForecastWeather.Cast cast : forecast.getCasts()) {
                                System.out.println(cast.toString());
                            }
                        }
                    }
                } else {
                    System.out.println("API请求失败，错误代码: " + weatherResponse.getInfocode());
                }
            } else {
                System.out.println("HTTP请求失败，状态码: " + response.statusCode());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
